﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
namespace WorkItemRSS
{
    /// <summary>
    /// 
    /// </summary>
    public class DAL
    {
        /// <summary>
        /// Updates the subscriber.
        /// </summary>
        /// <param name="LogonUserIdentityName">Name of the logon user identity.</param>
        /// <param name="AssignedTo">The assigned to.</param>
        /// <param name="ManagerName">Name of the manager.</param>
        /// <returns></returns>
        public int UpdateSubscriber(string LogonUserIdentityName, string AssignedTo, string ManagerName, bool IsDirectReportsUpdates)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetSqlStringCommand("subscriberInsert");
                dbCommand.CommandType = CommandType.StoredProcedure;
                db.AddInParameter(dbCommand, "LogonUserIdentityName", DbType.String, LogonUserIdentityName);
                db.AddInParameter(dbCommand, "displayName", DbType.String, AssignedTo);
                db.AddInParameter(dbCommand, "Manager", DbType.String, ManagerName);
                db.AddInParameter(dbCommand, "IsDirectReportsUpdates", DbType.Boolean, IsDirectReportsUpdates);
                
                return db.ExecuteNonQuery(dbCommand);
            }
            catch
            {
                return -1;
            }
        }
        /// Returns the detail associated with the CategoryId
        public DataSet GetFeed(string UserHostAddress, string AssignedTo, string WorkItemType, string tfsProjectName)
        {
            DataSet ds = new DataSet();

            Database db = DatabaseFactory.CreateDatabase();


            DbCommand dbCommand = db.GetSqlStringCommand("WorkItemGetBYAssignedTo");
            dbCommand.CommandType = CommandType.StoredProcedure;
            db.AddInParameter(dbCommand, "UserHostAddress", DbType.String, UserHostAddress);
            db.AddInParameter(dbCommand, "AssignedTo", DbType.String, AssignedTo);
            db.AddInParameter(dbCommand, "Type", DbType.String, WorkItemType);
            db.AddInParameter(dbCommand, "tfsProjectName", DbType.String, tfsProjectName);
            ds = db.ExecuteDataSet(dbCommand);

            return ds;
        }
        public int MarkFeedAsRead(long FeedSID)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = db.GetSqlStringCommand("WorkItem_update_ReadDate");
                dbCommand.CommandType = CommandType.StoredProcedure;
                db.AddInParameter(dbCommand, "SID", DbType.Int32, FeedSID);
              
                return db.ExecuteNonQuery(dbCommand);
            }
            catch
            {
                return -1;
            }
        }
        public string GetSubscriberByLogonUserIdentityName(string LogonUserIdentityName)
        {
            string AssignedTo = string.Empty;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand("subscriber_By_LogonUserIdentityName");
            dbCommand.CommandType = CommandType.StoredProcedure;
            db.AddInParameter(dbCommand, "LogonUserIdentityName", DbType.String, LogonUserIdentityName);

            object obj = db.ExecuteScalar(dbCommand);
            if (obj != null)
                AssignedTo = (string)obj;
            return AssignedTo;
        }
    }
}